package com.zx.Service;

import com.zx.Dao.Jdbc;
import com.zx.Moudel.Infant;
import com.zx.Moudel.Queue;
import com.zx.Moudel.Users;
import com.zx.Moudel.Vaccine;
import com.zx.until.TimeDifference;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * com.zx.Service
 * 26832
 * zx
 * &#064;description:
 * &#064;author: lxm
 * UserServiceImpl
 * &#064;create: 2024-06-21 16:57
 **/

public class UserServiceImpl implements UserService {
    @Override
    public List<Queue> selectAllQueue(String idcard,String vaccine) {
        List<Queue> list = new ArrayList<>();
        String sql = null;
        if(vaccine.equals("-1")){
            sql = "select * from queue where idcard =?";
        }else{
            sql = "select * from queue where idcard =? and room=?";
        }
        Connection con = null;
        PreparedStatement psmt = null;
        Jdbc jdbc = new Jdbc();
        ResultSet rs = null;
        try{
            con = jdbc.getCon();
            psmt = con.prepareStatement(sql);
            if(vaccine.equals("-1")){
                psmt.setString(1,idcard);
            }else{
                psmt.setString(1,idcard);
                psmt.setString(2,vaccine);
            }

            rs = psmt.executeQuery();
            while(rs.next()){
                Queue queue = new Queue();
                queue.setId(rs.getInt("id"));
                queue.setIdcard(rs.getString("idcard"));
                queue.setData(rs.getString("data"));
                queue.setState(rs.getString("state"));
                queue.setQueue_number(rs.getInt("queue_number"));
                queue.setRoom(rs.getString("room"));
                list.add(queue);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean ExistQueue(Infant infant) throws SQLException {
        String sql = "select * from queue where idcard = ? and state ='未接种' and data = ?";
        Jdbc jdbc = new Jdbc();
        Connection con = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        List<Queue> list = new ArrayList<>();
        try{
            con = jdbc.getCon();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,infant.getIdcard());
            psmt.setString(2,infant.getData());
            rs = psmt.executeQuery();
            while(rs.next()){
                Queue queue = new Queue();
                queue.setId(rs.getInt("id"));
                queue.setIdcard(rs.getString("idcard"));
                queue.setData(rs.getString("data"));
                queue.setState(rs.getString("state"));
                list.add(queue);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (psmt != null) {
                psmt.close();
            }
            jdbc.Close(con,null,rs);
        }
        return list.isEmpty();//返回true代表可以插入
    }

    @Override
    public boolean InsertQueue(String idcard, String vaccine, String date) throws SQLException {
        String sql = "insert into queue(state, idcard, data, room) values (?,?,?,?)";
        Connection con = null;
        PreparedStatement psmt = null;
        Jdbc jdbc = new Jdbc();
        int i =-1;
        try{
            con = jdbc.getCon();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,"未接种");
            psmt.setString(2,idcard);
            psmt.setString(3,date);
            psmt.setString(4,vaccine);
            i = psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (psmt != null) {
                psmt.close();
            }
            jdbc.Close(con,null,null);
        }
        return i>0;
    }

    @Override
    public boolean ExistChild(Infant infant) throws SQLException {
        //查询chid表，如果存在信息返回false
        String sql = "select * from child where pphone=? and uanme=? and idcard=?";
        Connection con = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        Jdbc jdbc = new Jdbc();
        List<Infant> list = new ArrayList<>();
        try{
            con = jdbc.getCon();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,infant.getPphone());
            psmt.setString(2,infant.getUname());
            psmt.setString(3,infant.getIdcard());
            rs = psmt.executeQuery();
            while(rs.next()){
                Infant infant1 = new Infant();
                infant1.setId(rs.getInt("id"));
                infant1.setUname(rs.getString("uanme"));
                infant1.setSex(rs.getString("sex"));
                infant1.setBorth(rs.getString("borth"));
                infant1.setIdcard(rs.getString("idcard"));
                infant1.setBelong(rs.getString("belong"));
                infant1.setPphone(rs.getString("pphone"));
                infant1.setRemark(rs.getString("remark"));
                list.add(infant1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            psmt.close();
            jdbc.Close(con,null,rs);
        }

        return list.isEmpty();
    }

    @Override
    public boolean InsertChild(Infant infant) throws SQLException {
        String sql = "insert into child(pphone, uanme, sex, borth, belong, idcard, remark) VALUES (?,?,?,?,?,?,?)";
        Connection con = null;
        PreparedStatement psmt = null;
        Jdbc jdbc = new Jdbc();
        int i = -1;
        try{
            con = jdbc.getCon();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,infant.getPphone());
            psmt.setString(2,infant.getUname());
            psmt.setString(3,infant.getSex());
            psmt.setString(4,infant.getBorth());
            psmt.setString(5,infant.getBelong());
            psmt.setString(6,infant.getIdcard());
            psmt.setString(7,"非本社区儿童");
            i = psmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            psmt.close();
            jdbc.Close(con,null,null);
        }
        return i>0;
    }

    @Override
    public boolean CalculationTime(String borth,String vaccine) throws SQLException {
        //borth格式为时间字符串，格式为"yyyy-MM-dd HH:mm:ss"
        //如果要修改计算格式，在TimeDifference类中修改
        //查询疫苗表,查看时间
        String sql = "select * from vaccine where vaccine='"+vaccine+"'";
        Jdbc jdbc = new Jdbc();
        Connection con = null;
        Statement smt = null;
        ResultSet rs = null;
        TimeDifference timeDifference = new TimeDifference();
        Vaccine vaccine1 = new Vaccine();
        try{
            con = jdbc.getCon();
            smt = con.createStatement();
            rs = smt.executeQuery(sql);
            while(rs.next()){
                vaccine1.setId(rs.getInt("id"));
                vaccine1.setVaccine(rs.getString("vaccine"));
                vaccine1.setDate(rs.getString("date"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            jdbc.Close(con,smt,rs);
        }
        //获取本地时间然后和传入的时间作差值然后和疫苗时间作比较，如果疫苗时间大于当前时间则返回false
        //1、计算本地时间和出生时间
        String date = timeDifference.calculateTimeDifference(borth);
        System.out.println(date);
        int result = timeDifference.compareDays(date,vaccine1.getDate());
        //如果返回的值大于0则返回true
        return result >= 0;
    }

    @Override
    public boolean selectChild(String belong, String idcard) throws SQLException {
        String sql = "select * from child where idcard ='"+idcard+"' and belong ='C'";
        Connection con  = null;
        Statement smt = null;
        ResultSet rs = null;
        Jdbc jdbc = new Jdbc();
        List<Infant> list = new ArrayList<>();
        try{
            con = jdbc.getCon();
            smt = con.createStatement();
            rs = smt.executeQuery(sql);
            while(rs.next()){
                Infant infant = new Infant();
                infant.setId(rs.getInt("id"));
                infant.setUname(rs.getString("uanme"));
                infant.setSex(rs.getString("sex"));
                infant.setBorth(rs.getString("borth"));
                infant.setBelong(rs.getString("belong"));
                infant.setIdcard(rs.getString("idcard"));
                infant.setPphone(rs.getString("pphone"));
                infant.setRemark(rs.getString("remark"));
                list.add(infant);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jdbc.Close(con,smt,rs);
        }
        int i = list.size();
        if(i>0){
            return true;//是本社区的儿童
        }else{
         return false;//非本社区儿童
        }
    }

    @Override
    public int login(String username, String password) throws SQLException {
        String sql = "select * from users where uname='"+username+"' and password='"+password+"'";
        Jdbc jdbc = new Jdbc();
        Connection con = null;
        Statement smt = null;
        ResultSet rs = null;
        List<Users> list = new ArrayList<>();
        try{
            con= jdbc.getCon();
            smt = con.createStatement();
            rs = smt.executeQuery(sql);
            while(rs.next()){
                Users users = new Users();
                users.setId(rs.getInt("id"));
                users.setUname(rs.getString("uname"));
                users.setPassword(rs.getString("password"));
                users.setPhone(rs.getString("phone"));
                users.setRole(rs.getString("role"));
                list.add(users);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jdbc.Close(con,smt,rs);
        }
        if (!list.isEmpty()){
            if(list.get(0).getRole().equals("admin")){
                return 1;//管理员
            }else{
                return 2;//普通用户
            }
        }else{
            return 0;//登录失败
        }
    }
}
